package site.xuxingcan.autoclick;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by xuxingcan on 2016/9/12.
 */
public class ClickService extends Service {
    private ActivityManager mActivityManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private Context mContext;
    private int times = 0;

    @Override
    public void onCreate() {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);
        // 通过Notification.Builder来创建通知，注意API Level
        // API11之后才支持
        Notification notify2 = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏中的小图片，尺寸一般建议在24×24，这个图片同样也是在下拉状态栏中所显示，如果在那里需要更换更大的图片，可以使用setLargeIcon(Bitmap
                // icon)
                .setTicker("TickerText:" + "您有新短消息，请注意查收！")// 设置在status
                // bar上显示的提示文字
                .setContentTitle("模拟点击")// 设置在下拉status
                // bar后Activity，本例子中的NotififyMessage的TextView中显示的标题
                .setContentText("模拟点击运行中...")// TextView中显示的详细内容
//                .setContentIntent(pendingIntent2) // 关联PendingIntent
                .setNumber(1) // 在TextView的右方显示的数字，可放大图片看，在最右侧。这个number同时也起到一个序列号的左右，如果多个触发多个通知（同一ID），可以指定显示哪一个。
                .getNotification(); // 需要注意build()是在API level
        // 16及之后增加的，在API11中可以使用getNotificatin()来代替
//        notify2.flags |= Notification.FLAG_AUTO_CANCEL;
        manager.notify(1, notify2);
        mContext = this;
        mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        super.onCreate();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    boolean isCon = false;
                    while (true) {
                        for (MainActivity.APPInfo mAppInfo : MainActivity.AppList) {
                            mActivityManager.killBackgroundProcesses(mAppInfo.packageName);
                            Thread.sleep(3000);
                            Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(mAppInfo.packageName);
                            mContext.startActivity(intent);
                            times = 0;
                            isCon = false;
                            while (true) {
                                times++;
                                if (times > 20) {
                                    isCon = true;
                                    break;
                                }
                                String activity = RootShell.getTopActivity();
                                if (activity != null && activity.contains(mAppInfo.ActivityName)) {
                                    break;
                                }
                                Thread.sleep(500);
                            }
                            if (isCon) {
                                continue;
                            }
                            times = 0;
                            isCon = false;
                            while (true) {
                                times++;
                                String activity = RootShell.getTopActivity();
                                if (activity == null) {
                                    continue;
                                }
                                if (activity.contains(mAppInfo.ActivityName)) {
                                    if (times % 2 == 0) {
                                        RootShell.simulateTap(300, 185);
                                    } else {
                                        RootShell.simulateTap(465, 185);
                                    }
                                    if (times > 10) {
                                        isCon = true;
                                        break;
                                    }
                                } else {
                                    break;
                                }
                                Thread.sleep(3000);
                            }
                            if (isCon) {
                                continue;
                            }
                            times = 0;
                            while (true) {
                                times++;
                                if (times > 20) {
                                    break;
                                }
                                String activity = RootShell.getTopActivity();
                                if (activity != null && activity.contains("com.google.android.finsky.activities.MainActivity")) {
                                    break;
                                }
                                Thread.sleep(1000);
                            }
                            Thread.sleep(3000);
                            mActivityManager.killBackgroundProcesses(mAppInfo.packageName);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
